package api

import (
	"fmt"
	"runtime/debug"
	"strconv"

	"github.com/deploy-services/v2/KolonseWeb/HttpLib"
	"github.com/deploy-services/v2/KolonseWeb/Type"
	"github.com/deploy-services/v2/services"
	// "github.com/deploy-services/v2/services"
)

func public_history(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.ShowPublicHistory(req.URL.Query().Get("name"))
	res.Write([]byte(rest))
}

func public_show(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	rest := services.PublicShow(req.URL.Query().Get("name"))
	res.Write([]byte(rest))
}

func public_commit(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	force, _ := strconv.ParseBool(req.URL.Query().Get("force"))
	rest := services.PublicCommit(name, force)
	res.Write([]byte(rest))
}

func public_active(req *HttpLib.Request, res *HttpLib.Response, next Type.Next) {
	defer func() {
		if r := recover(); r != nil {
			res.Write([]byte(fmt.Sprintln(r)))
			res.Write(debug.Stack())
		}
	}()
	name := req.URL.Query().Get("name")
	commit := req.URL.Query().Get("commit")
	if len(commit) == 0 {
		commit = "latest"
	}
	rest := services.PublicActive(name, commit)
	res.Write([]byte(rest))
}
